/*
Copyright (c) 2004-2008,

Sebastian Ryszard Kruk
http://www.sebastiankruk.com/

with contributions from (under the Corrib.org project):
Digital Enterprise Research Institute, 
National University of Ireland, Galway, Ireland
http://www.deri.org/

Faculty of Electronics, Telecommunication and Informatics and Main Library
Gdansk University of Technology, Poland
http://www.eti.pg.gda.pl/
http://www.bg.pg.gda.pl/


FOAFRealm is distributed under New BSD License.


All rights reserved.


Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, 
  this list of conditions and the following disclaimer in the documentation and/or 
  other materials provided with the distribution and reference to the source code. 
* Neither the name of Sebastian Ryszard Kruk nor the names of its contributors 
  (Digital Enterprise Research Institute, National University of Ireland, Galway, Ireland; 
  Faculty of Electronics, Telecommunication and Informatics and Main Library Gdansk University of Technology, Poland) 
  may be used to endorse or promote products derived from this software without specific prior written permission.


THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


package cn.sh.library.gmwx.utils;

import java.security.InvalidParameterException;
import java.util.Random;

/**
 * Generates random strings.
 * 
 * @author Paweł Bugalski, Marisz Cygan, Piotr Piotrowski, Lukasz Korczynski
 */
public class IdEngine {

	private static final char[] chars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

	private static final char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

	private static Random random = new Random();

	/**
	 * Generates random string with chars (1-st only digit, 2-8 chars)
	 * 
	 * @param length
	 * @return String
	 */
	public static String gerateId() {
		StringBuilder string = new StringBuilder();
		string.append(digits[random.nextInt(digits.length)]);
		
		for (int i = 1; i < 8; i++) {
			string.append(chars[random.nextInt(chars.length)]);
		}

		return string.toString();
	}
	
	
	public static String generateMultiplesOfId(int multiples){
		StringBuilder string = new StringBuilder();
		if(multiples < 1){
			throw new InvalidParameterException();
		}
		for(int i=0; i<multiples; i++){
			string.append(gerateId());
		}
		return string.toString();
	}
	
	/**
	 * Generates random string with chars
	 * 
	 * @param length
	 * @return String
	 */
	public static String randomChars(int length) {
		StringBuilder string = new StringBuilder();
		for (int i = 0; i < length; i++) {
			string.append(chars[random.nextInt(chars.length)]);
		}

		return string.toString();
	}

	/**
	 * Generates random string with digits
	 * 
	 * @param length
	 * @return String
	 */
	public static String randomDigits(int length) {
		StringBuilder string = new StringBuilder();
		for (int i = 0; i < length; i++) {
			string.append(digits[random.nextInt(digits.length)]);
		}

		return string.toString();
	}
	
}


